package com.zdkj.subway.check.controller.subwayRat.conductorDifferences.passengersTransaction;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.core.util.R;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.check.entity.Page;
import com.zdkj.subway.check.entity.SwStApplyNetworkCheckPassengerAffairs;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwStApplyNetworkCheckPassengerAffairsMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.service.impl.subwayRat.conductorDifferences.passengersTransaction.SwStApplyNetworkCheckPassengerAffairsServiceImpl;
import com.zdkj.subway.check.util.LineOrStation;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表差错信息
 */
@RestController
@RequestMapping("/check")
@Slf4j
public class SwStApplyNetworkCheckPassengerAffairsController {

    @Autowired
    private SwStApplyNetworkCheckPassengerAffairsServiceImpl swStApplyNetworkCheckPassengerAffairsService;
    @Autowired
    SwStApplyNetworkCheckPassengerAffairsMapper swStApplyNetworkCheckPassengerAffairsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 车站提交事务协查审核
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/getSwStApplyNetworkCheckPassengerAffairs")
    @ApiOperation(value = "车站提交事务协查审核", notes = "车站提交事务协查审核")
    public R getSwStApplyNetworkCheckPassengerAffairs(com.baomidou.mybatisplus.extension.plugins.pagination.Page page,@RequestParam Map<String,Object> param){
        /*List<Map<String, Object>> swStApplyNetworkCheckPassengerAffairs = swStApplyNetworkCheckPassengerAffairsService.getSwStApplyNetworkCheckPassengerAffairs(param);
        page.setData(swStApplyNetworkCheckPassengerAffairs);*/
        PigxUser user = SecurityUtils.getUser();
        if(user.getBelongToLine()){
            List lineList = LineOrStation.getmanagementLine();
            param.put("lines",lineList);
        }else{
            List lineList = LineOrStation.getLineOrManagementLine();
            param.put("lines",lineList);
        }
        IPage<Map<String, Object>> swStApplyNetworkCheckPassengerAffairs = swStApplyNetworkCheckPassengerAffairsService.getSwStApplyNetworkCheckPassengerAffairs(page,param);
        return R.ok(swStApplyNetworkCheckPassengerAffairs);
    }

    /**
     * 编辑
     * @param param
     * @return
     */
    @RequestMapping("/updateSwStApplyNetworkCheckPassengerAffairs")
    @ApiOperation(value = "编辑", notes = "编辑")
    public Result updateSwStApplyNetworkCheckPassengerAffairs(@RequestParam Map<String,String> param){
        if(null != param.get("ids")){
            List<Integer> ids = (List)JSONObject.parseArray(String.valueOf(param.get("ids")));
            for(Integer id : ids){
                SwStApplyNetworkCheckPassengerAffairs query = swStApplyNetworkCheckPassengerAffairsMapper.selectById(id);
                if(null != query && "2".equals(query.getLineStatus())){
                    SwStApplyNetworkCheckPassengerAffairs swStApplyNetworkCheckPassengerAffairs = new SwStApplyNetworkCheckPassengerAffairs();
                    swStApplyNetworkCheckPassengerAffairs.setId(Long.valueOf(id));
                    swStApplyNetworkCheckPassengerAffairs.setLineStatus(param.get("lineStatus"));
                    swStApplyNetworkCheckPassengerAffairs.setLineFile(param.get("lineFile"));
                    swStApplyNetworkCheckPassengerAffairsMapper.updateById(swStApplyNetworkCheckPassengerAffairs);
                }
            }
        }else{
            SwStApplyNetworkCheckPassengerAffairs swStApplyNetworkCheckPassengerAffairs = new SwStApplyNetworkCheckPassengerAffairs();
            swStApplyNetworkCheckPassengerAffairs.setId(Long.valueOf(param.get("id")));
            swStApplyNetworkCheckPassengerAffairs.setReturnCreatorName(param.get("returnCreatorName"));
            swStApplyNetworkCheckPassengerAffairs.setReturnCreatorCode(param.get("returnCreatorCode"));
            swStApplyNetworkCheckPassengerAffairs.setReturnData(param.get("returnData"));
            //swStApplyNetworkCheckPassengerAffairs.setLineStatus(param.get("lineStatus"));
            swStApplyNetworkCheckPassengerAffairs.setStationFile(param.get("stationFile"));
            swStApplyNetworkCheckPassengerAffairs.setLineFile(param.get("lineFile"));
            swStApplyNetworkCheckPassengerAffairsMapper.updateById(swStApplyNetworkCheckPassengerAffairs);
        }
        Result res=new Result(ResultDetailMsg.OK);
        return res;
    }

    /**
     * 提交
     * @param ids
     * @return
     */
    @RequestMapping("/submitFeedback")
    @ApiOperation(value = "提交", notes = "提交")
    public R submitFeedback (@Valid @RequestBody List<Long> ids){
        boolean saveFlag = false;
        for(Long item : ids){
            SwStApplyNetworkCheckPassengerAffairs swStApplyNetworkCheckPassengerAffairs = new SwStApplyNetworkCheckPassengerAffairs();
            swStApplyNetworkCheckPassengerAffairs.setId(item);
            swStApplyNetworkCheckPassengerAffairs.setReturnTime(new Date());
            /*swStApplyNetworkCheckPassengerAffairs.setReturnCreatorName(SecurityUtils.getUser().getNikeName());
            swStApplyNetworkCheckPassengerAffairs.setReturnCreatorCode(SecurityUtils.getUser().getUserCode());*/
            //反馈状态：已回馈
            swStApplyNetworkCheckPassengerAffairs.setLineStatus("1");
            int saves = swStApplyNetworkCheckPassengerAffairsMapper.updateById(swStApplyNetworkCheckPassengerAffairs);
            if(saves>0){
                saveFlag =true;
            }
        }
        return R.ok(saveFlag);
    }

    /**
     * 获取线网人员
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/getSysUserToLine")
    @ApiOperation(value = "获取线网人员", notes = "获取线网人员")
    public Page getSysUserToLine(Page page,@RequestParam Map<String,String> param){
        Map<String,Object> p=new HashMap<>();
        p.put("belong_to_line","1");
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p);
        page.setData(sysUsers);
        return page;
    }

    /**
     * 获取线网人员
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/getSysUserToLineSelect")
    @ApiOperation(value = "获取线网人员", notes = "获取线网人员")
    public List<SysUser> getSysUserToLineSelect(Page page,@RequestParam Map<String,String> param){
        Map<String,Object> p=new HashMap<>();
        p.put("belong_to_line","1");
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p);
        return sysUsers;
    }

}
